<template>
  <div>
    <selectRoute ref="selectRoute" :name="name" @handleSelect="routeHandleSelect"></selectRoute>
    <!-- 内容区域样式 -->
    <!-- <div v-if="userModel.role=='admin'||changeOpenshopStatus=='Y'">{{nodeModel.id}}</div> -->
    <placeBanner v-if="name=='banner'" :nodeModel="nodeModel" @insertAdvert="insertAdvert" @selectRoute="selectRoute" @deleteAdvert="deleteAdvert"></placeBanner>
    <placeGrid v-if="name=='grid'" :nodeModel="nodeModel" @insertAdvert="insertAdvert" @selectRoute="selectRoute" @deleteAdvert="deleteAdvert"></placeGrid>
    <placeNoticebar v-if="name=='noticebar'" :nodeModel="nodeModel" @insertAdvert="insertAdvert" @deleteAdvert="deleteAdvert"></placeNoticebar>
    <placeRichtext v-if="name=='richtext'" :nodeModel="nodeModel" @insertAdvert="insertAdvert" @deleteAdvert="deleteAdvert"></placeRichtext>
    <placeItemGrid v-if="name=='itemGrid'||name=='itemList'" :name="name" :nodeModel="nodeModel" @insertAdvert="insertAdvert" @selectRoute="selectRoute" @deleteAdvert="deleteAdvert"></placeItemGrid>
    <placeShare v-if="name=='share'" :nodeModel="nodeModel" @selectRoute="selectRoute"></placeShare>
    <placeItemBaseinfo v-if="name=='item-baseinfo'" :nodeModel="nodeModel"></placeItemBaseinfo>
    <placeItemDetail v-if="name=='item-detail'" :nodeModel="nodeModel"></placeItemDetail>
    <placeItemCate v-if="name=='itemCate'" :nodeModel="nodeModel" @insertAdvert="insertAdvert" @selectRoute="selectRoute" @deleteAdvert="deleteAdvert"></placeItemCate>
    <placeCustom v-if="name=='custom'" :nodeModel="nodeModel"></placeCustom>
  </div>
</template>

<script>
import {
  findItemListRemoveActivity
} from '@/api/merchant/item.js'
import selectRoute from './components/selectRoute'
import {count} from '@/utils/count.js'
import {tools} from '@/utils/tools.js'
import custom from '@/components/jcupload/custom'
import placeBanner from '@/views/page/composing/placeRight/place-banner'
import placeGrid from '@/views/page/composing/placeRight/place-grid'
import placeNoticebar from '@/views/page/composing/placeRight/place-noticebar'
import placeRichtext from '@/views/page/composing/placeRight/place-richtext'
import placeItemGrid from '@/views/page/composing/placeRight/place-itemGrid'
import placeShare from '@/views/page/composing/placeRight/place-share'
import placeItemBaseinfo from '@/views/page/composing/placeRight/place-item-baseinfo'
import placeItemDetail from '@/views/page/composing/placeRight/place-item-detail'
import placeItemCate from '@/views/page/composing/placeRight/place-itemCate'
import placeCustom from '@/views/page/composing/placeRight/place-custom'
export default {
  components: {
    selectRoute,
    custom,
    placeBanner,
    placeGrid,
    placeNoticebar,
    placeRichtext,
    placeItemGrid,
    placeShare,
    placeItemBaseinfo,
    placeItemDetail,
    placeItemCate,
    placeCustom
  },
  props:{
    name: {
      type: String,
      default : ''
    },
    nodeModel:{
        type: Object,
        default: ()=>{}
      },
  },
  data() {
    return {
      selectAdert: {}, //标记选择的advert
      userModel: {},
      changeOpenshopStatus: ''
    }
  },
  created(){
    this.userModel = this.$store.state.user.userModel
    this.changeOpenshopStatus = localStorage.getItem('changeOpenshopStatus')
    console.log('changeOpenshopStatus', this.changeOpenshopStatus)
  },
  methods:{
    // 追加advert
    insertAdvert(e){
      console.log('追加', e)
      if(e.name=='itemGrid'){
        var newAdvert = {
          itemName: '',
          coverUrl: '',
          price: '',
          marketPrice: '',
          sales: '',
          itemModel: {},
        }
        this.nodeModel.advertList.push(newAdvert)
      }else{
        var newAdvert = {
          title: '',
          content: '',
          coverUrl: '',
          link: '',
          itemModel: {},
          types: e.types,
          filter: ''
        }
        console.log('this.nodeModel', this.nodeModel)
        if(this.nodeModel.advertList.length>0){
          newAdvert.sort = count.add(this.nodeModel.advertList[this.nodeModel.advertList.length-1].sort, 1)
        }else{
          newAdvert.sort = 0
        }
        
        this.nodeModel.advertList.push(newAdvert)
        console.log('this.nodeModel2', this.nodeModel)
      }
      
    },
    //删除advert
    deleteAdvert(e){
      this.$alert('确定删除吗？', {
        confirmButtonText: '确定',
        callback: action => {
          console.log(action)
          if(action == 'confirm'){
            this.nodeModel['deleteAdvert'] = true
            this.nodeModel.advertList.splice(e, 1)
          }
        }
      })
    },
    // 选择路由
    selectRoute(e, types){
      console.log('选择', e, types)
      this.selectAdert = e
      this.selectAdert.types = types
      // 处理回显富文本
      var link = {}
      if(this.selectAdert.link){
        link = JSON.parse(this.selectAdert.link)
      }
      if(link.name=='protocolView' && this.selectAdert.query){
        this.$refs.selectRoute.richtext = JSON.parse(this.selectAdert.query).content
      }
      this.$refs.selectRoute.show()
    },
    // 选择路由回调
    routeHandleSelect(link, query){
      this.selectAdert.link = link
      this.selectAdert.query = query
    },

    routeHandleSelect1(e, type){
      console.log('选择路由', e, type, this.selectAdert)
      switch (type) {
        case 'itemFilter':
          if(this.name=='itemList'){
            this.nodeModel.filter = e
            this.findItemListRemoveActivity(this.nodeModel.filter)
          }else{
            this.selectAdert.link = 'name=itemList&modal=push&type=item'
            this.selectAdert.filter = e
          }
          break;
        case 'item':
          if(this.name=='itemGrid'){
            this.selectAdert.item = e.id
            this.selectAdert.itemModel = e
          }else{
            this.selectAdert.title = e.itemName
            this.selectAdert.link = 'name=itemDetail&modal=push&itemId='+e.id
            this.selectAdert.filter = 'itemId='+e.id
          }
          break;
        case 'activityItem':
          if(this.name=='itemGrid'){
            this.selectAdert.item = e.id
            this.selectAdert.itemModel = e
          }else{
            this.selectAdert.title = e.itemName
            this.selectAdert.link = 'name=itemDetail&modal=push&itemId='+e.id
            this.selectAdert.filter = 'itemId='+e.id
          }
          break;
      
        default:
          break;
      }
    },
    // 查询商品列表
    findItemListRemoveActivity(filter) {
      var searchParams = {}
      searchParams.page = 1
      searchParams.size = 6
      searchParams = Object.assign(tools.paramsToObj(filter), searchParams)
      findItemListRemoveActivity(searchParams).then(res => {
        if(res.data.code == 10000){
          this.nodeModel.itemList = res.data.data.list
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.row{
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-left: 20px;
  margin-top: 10px;
}
</style>
    